package com.example.day02.parnterfrag;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;

import com.example.day02.R;
import com.example.day02.startfrag.StarInfo;
import com.example.day02.utils.AssetsUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * A simple {@link Fragment} subclass.
 */
public class ParnterFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemSelectedListener {
    Spinner boySp,girlSp;
    ImageView boyIv,girlIv;
    Button luckBtn,analysisBtn;
    List<String>nameList;

    private List<StarInfo.StarinfoBean> infoList;
//    String boyStar[]={"白羊座","金牛座","双子座","巨蟹座","狮子座","处女座","天秤座","天蝎座","射手座","摩羯座","水瓶座","双鱼座"};
//    String girlStar[]={"白羊座","金牛座","双子座","巨蟹座","狮子座","处女座","天秤座","天蝎座","射手座","摩羯座","水瓶座","双鱼座"};
    ArrayAdapter<String> boyAdapter;
    ArrayAdapter<String> girlAdapter;



    public ParnterFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        Bundle bundle = getArguments();
        nameList=new ArrayList<>();
        StarInfo Info= (StarInfo) bundle.getSerializable("info");
        infoList = Info.getStarinfo();
        for (int i = 0; i < infoList.size(); i++) {
          nameList.add(infoList.get(i).getName());
        }

        //初始化控件
        View view=inflater.inflate(R.layout.fragment_parnter, container, false);
        boyIv=view.findViewById(R.id.parnter_boy_iv);
        girlIv=view.findViewById(R.id.parnter_girl_iv);
        boySp=view.findViewById(R.id.parnter_boy_sp);
        girlSp=view.findViewById(R.id.parnter_girl_sp);
        luckBtn=view.findViewById(R.id.parnter_btn_luck);
        analysisBtn=view.findViewById(R.id.parnter_btn_Analysis);
        //初始化.设置适配器
        boyAdapter = new ArrayAdapter<>(getContext(), R.layout.item_sp, R.id.item_sp_tv, nameList);
        girlAdapter = new ArrayAdapter<>(getContext(), R.layout.item_sp, R.id.item_sp_tv, nameList);
        boySp.setAdapter(boyAdapter);
        girlSp.setAdapter(girlAdapter);
        Map<String, Bitmap> contentlogoImgMap = AssetsUtils.getContentLogoImgMap();
        String logoname = infoList.get(0).getLogoname();
        Bitmap bitmap = contentlogoImgMap.get(logoname);
        boyIv.setImageBitmap(bitmap);
        girlIv.setImageBitmap(bitmap);
        boySp.setOnItemSelectedListener(this);
        girlSp.setOnItemSelectedListener(this);
        analysisBtn.setOnClickListener(this);
        return view;

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.parnter_btn_luck://跳转到抽奖页面
                break;
            case R.id.parnter_btn_Analysis://跳转到配对页面
                //获取被选中位置
                int boyPosition = boySp.getSelectedItemPosition();
                int girlPosition = girlSp.getSelectedItemPosition();
                Intent intent = new Intent(getContext(),MatchActivity.class);
                intent.putExtra("boy_name",infoList.get(boyPosition).getName());
                intent.putExtra("boy_logo",infoList.get(boyPosition).getLogoname());
                intent.putExtra("girl_name",infoList.get(girlPosition).getName());
                intent.putExtra("girl_logo",infoList.get(girlPosition).getLogoname());
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        switch (parent.getId()){
            case R.id.parnter_boy_sp:
                Map<String, Bitmap> BoycontentlogoImgMap = AssetsUtils.getContentLogoImgMap();
                String logonameBoy = infoList.get(position).getLogoname();
                Bitmap bitmap = BoycontentlogoImgMap.get(logonameBoy);
                boyIv.setImageBitmap(bitmap);

                break;
            case R.id.parnter_girl_sp:
                Map<String, Bitmap> GirlcontentlogoImgMap = AssetsUtils.getContentLogoImgMap();
                String logonameGirl = infoList.get(position).getLogoname();
                Bitmap bitmapGirl = GirlcontentlogoImgMap.get(logonameGirl);
                girlIv.setImageBitmap(bitmapGirl);
                break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}
